package com.wyl.elk.controller;

import com.alibaba.fastjson.JSON;
import com.wyl.elk.dto.WatcherDTO;
import com.wyl.elk.util.InputMDC;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: wyl
 * @Date: 2021/7/7
 * @Description:
 */
@Slf4j
@RestController
public class TestController {


    /**
     * 日志格式配置：
     * [%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ}]
     * [%level{length=5}]
     * [%thread-%tid]
     * [%logger]
     * [%X{hostName}]
     * [%X{ip}]
     * [%X{applicationName}]
     * [%F,%L,%C,%M]
     * [%m] ## '%ex'%n
     * <p>
     * 日志输出内容：
     * [2021-07-07T08:53:37.515+08:00]
     * [INFO]
     * [http-nio-8081-exec-9-44]
     * [com.wyl.elk.controller.TestController]
     * [wyl]
     * [172.31.80.125]
     * [collector]
     * [TestController.java,21,com.wyl.elk.controller.TestController,test]
     * [这是一条info日志] ## ''
     *
     * @return
     */
    @GetMapping(value = "/test")
    public String test() {
        InputMDC.putMDC();

        log.info("这是一条info日志");

        log.warn("这是一条warn日志");

        log.error("这是一条error日志");

        return "test";
    }


    @GetMapping(value = "/err")
    public String err() {
        InputMDC.putMDC();
        try {
            int a = 1 / 0;
        } catch (Exception e) {
            log.error("算术异常", e);
        }
        return "err";
    }

    /**
     * 接收告警信息
     *
     * @param watcherDTO
     * @return
     */
    @PostMapping(value = "accurateWatch")
    public String accurateWatch(@RequestBody WatcherDTO watcherDTO) {

        System.out.println(JSON.toJSONString(watcherDTO));
        return "watcher";
    }
}
